package com.yvdelx.cyclowomen.activity.listener;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.Toast;

import com.yvdelx.R;
import com.yvdelx.cyclowomen.activity.CalendarActivity;
import com.yvdelx.cyclowomen.activity.DayConsultationActivity;
import com.yvdelx.cyclowomen.exception.CstException;
import com.yvdelx.cyclowomen.exception.FunctionalException;
import com.yvdelx.cyclowomen.mediation.CycleManagement;
import com.yvdelx.cyclowomen.object.Day;
import com.yvdelx.cyclowomen.object.EnumSensation;
import com.yvdelx.cyclowomen.utility.ActivityUtility;
import com.yvdelx.cyclowomen.utility.DateUtility;
import com.yvdelx.cyclowomen.utility.LogUtility;
import com.yvdelx.cyclowomen.utility.StringUtility;
import com.yvdelx.cyclowomen.utility.ViewUtility;

import static com.yvdelx.cyclowomen.utility.StringUtility.CST_BUNDLE;
import static com.yvdelx.cyclowomen.utility.StringUtility.CST_DATE;

/**
 * DayActivityListener
 * Created by ydelvaux on 04/03/14.
 */
public class DayActivityListener extends AbstractActivityListener {

    // IDs from the R.id
    private final static Integer CST_ID_TEMPERATURE_EDIT_TEXT = R.id.temperatureEditText;
    private final static Integer CST_ID_COMMENT_EDIT_TEXT = R.id.commentEditText;


    private Day day;

    public DayActivityListener(Day day, Activity activity){
        this.day = day;
        this.activity = activity;
    }

    @Override
    public void onClick(View view) {
        // Determine witch view has been clicked
        switch(view.getId()) {
            case R.id.NextDayButton:
                onClickDayConsultation(CycleManagement.getNextDay(day,true));
                break;
            case R.id.PreviousDayButton:
                onClickDayConsultation(CycleManagement.getNextDay(day,false));
                break;
            case R.id.ValidateDayModificationButton:
                onClickDayModification();
                break;

            case R.id.question_pok_button:
                onClickQuestionPOK();
                break;
        }
    }

    /**
     * Action then user click on pok question
     */
    private void onClickQuestionPOK() {
        assert this.activity.getApplicationContext() != null;
        Toast.makeText(this.activity.getApplicationContext(),
                R.string.pok_explanation,Toast.LENGTH_LONG).show();
    }

    /**
     * Action then user select next or previous day
     * @param daySelect Day
     */
    private void onClickDayConsultation(Day daySelect){
        Context context = this.activity.getApplicationContext();
        if(daySelect != null){
            // Choose between Day Modification and Day Consultation
            Intent intent = new Intent(context, DayConsultationActivity.class);
            Bundle bundle = new Bundle();
            bundle.putLong(CST_DATE, DateUtility.getLongFromDate(daySelect.getDate()));
            intent.putExtra(CST_BUNDLE, bundle);
            if(context != null)
                this.activity.startActivity(intent);
        } else {
            assert context != null;
            Toast.makeText(context,R.string.day_not_exist,Toast.LENGTH_LONG).show();
        }
    }

    /**
     * Action then the user want modify the day data
     */
    private void onClickDayModification(){
        // Get the temperature
        Float temperature = ViewUtility
                .getLongEditText(this.activity.findViewById(CST_ID_TEMPERATURE_EDIT_TEXT));
        // Get the comment
        String comment = ViewUtility.getStringEditText(this.activity.findViewById(CST_ID_COMMENT_EDIT_TEXT));
        // Get the sensation
        EnumSensation sensation = EnumSensation.DRY;
        Spinner spinner = (Spinner) this.activity.findViewById(R.id.SensationChoiceSpinner);
        String sensationStr = String.valueOf(spinner.getSelectedItem());
        if(sensationStr.equals(this.activity.getResources().getString(R.string.Menses))){
            sensation = EnumSensation.MENSES;
        } else if(sensationStr.equals(this.activity.getResources().getString(R.string.Humid))){
            sensation = EnumSensation.HUMID;
        } else if(sensationStr.equals(this.activity.getResources().getString(R.string.Wet))){
            sensation = EnumSensation.WET;
        } else if(sensationStr.equals(this.activity.getResources().getString(R.string.Lubricate))){
            sensation = EnumSensation.LUBRICATE;
        }

        // Get the quantity of sensation
        float quantity = ViewUtility.getNumStarFilled(this.activity.findViewById(R.id.QuantityRatingBar));

        // Get if the measure has been taken in nominal condition
        boolean isNominalCondition = ViewUtility.getValueCheckBox(
                this.activity.findViewById(R.id.pok_check_box));

        try {
            // Modify the day with the data from previous get
            this.day = new Day(this.day.getDate(), temperature, sensation, comment, quantity, isNominalCondition);
            CycleManagement.modifyDay(this.day);

            // Return on the calendar activity
            Intent intent = new Intent(this.activity, CalendarActivity.class);
            this.activity.startActivityForResult(intent, ActivityUtility.CST_CALENDAR_ACTIVITY);
        } catch (FunctionalException e) {
            if(StringUtility.compareStrings(e.getCode(), CstException.CST_FIRST_DAY_CYCLE_NOT_MENSES)) {
                //noinspection ConstantConditions
                Toast.makeText(this.activity.getApplicationContext(), R.string.fistDayCycleMenses,
                        Toast.LENGTH_LONG).show();
            } else {
                LogUtility.error(this.getClass(), e.getMessage(), e);
                this.activity.finish();
            }
        } catch (Exception e) {
            LogUtility.error(this.getClass(), e.getMessage(), e);
            this.activity.finish();
        }
    }
}
